package com.wwj.admin.controller;


import com.wwj.shiro.SessionUser;
import com.wwj.shiro.ShiroUser;
import com.wwj.core.system.service.IMenuService;
import com.wwj.framework.controller.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xya on 16-10-24.
 */
@Controller
@RequestMapping
public class HomeController extends BaseController {

    public static final String INDEX_URL = "/index";

    public static final String HOME_URL_ADMIN = "/admin/home";
    public static final String HOME_URL_USER = "/biz/home";
    @Value("#{resourceProperties['prefix.screen.url']}")
    private String screenUrl;

    @Resource
    private IMenuService menuService;

    /**
     * 首页的映射
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = {"", "/", "/index"})
    public String index(Model model) throws Exception {
        //获取当前登录人
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if (user == null || user.getId().equals("")) {
            return null;
        }
        List<Map<String, Object>> menus = null;
        try {
            menus = menuService.findMenuByUser(user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("menus", menus);
        model.addAttribute("username", null == SessionUser.getUserName()?SessionUser.getBarName():SessionUser.getUserName());
        /*if(user.getType().equals(1))
            return new ModelAndView(HOME_URL_ADMIN);
        else if(user.getType().equals(2))
            return new ModelAndView(HOME_URL_USER);
        else
            return new ModelAndView(ERROR);*/
        return INDEX_URL;
    }

    /**
     * 首页
     *
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/home")
    public ModelAndView home(Model model) throws Exception {
        //获取当前登录人
        ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        if(user.getType().equals(1)) {
            return new ModelAndView(HOME_URL_ADMIN);
        }else {
            return new ModelAndView(ERROR);
        }
    }

}
